using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;

namespace DAL
{
   public class tbBinhluanDAL
    {
        private SqlConnection cn = null;
        public tbBinhluanDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(tbBinhluan bl)
        {
            SqlCommand cm = new SqlCommand("Binhluan_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Makh", bl.Makh);
            cm.Parameters.AddWithValue("@Masp", bl.Masp);
            cm.Parameters.AddWithValue("@Noidung", bl.Noidung);
            cm.Parameters.AddWithValue("@Ngaygui", bl.Ngaygui);
            cm.ExecuteNonQuery();
        }
        public void Update(tbBinhluan bl)
        {
            SqlCommand cm = new SqlCommand("Binhluan_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Mabl", bl.Mabl );
            cm.Parameters.AddWithValue("@Makh", bl.Makh);
            cm.Parameters.AddWithValue("@Masp", bl.Masp);
            cm.Parameters.AddWithValue("@Noidung", bl.Noidung);
            cm.Parameters.AddWithValue("@Ngaygui", bl.Ngaygui);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("Binhluan_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Mabl", iD);
            cm.ExecuteNonQuery();
        }
        public tbBinhluanCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("Binhluan_Get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbBinhluanCollection tbblCollection = new tbBinhluanCollection();
            while (dr.Read()) 
            {
                tbBinhluan bl = new tbBinhluan();
                bl.Mabl = int.Parse(dr.GetValue(0).ToString());
                bl.Makh = int.Parse(dr.GetValue(1).ToString());
                bl.Masp = int.Parse(dr.GetValue(2).ToString());
                bl.Noidung= dr.GetValue(3).ToString();
                bl.Ngaygui = DateTime.Parse( dr.GetValue(4).ToString());
                tbblCollection.Add(bl);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return tbblCollection;
        }
    }
}
